<template>
	<view class="container">


		<Header></Header>

		<!-- Main Content - Registration Form -->
		<view class="main-content">
			<text class="page-title">用户注册</text>

			<view class="form-section">
				<view class="form-item">
					<text class="form-label">手机号码:</text>
					<view class="input-wrapper">
						<input class="form-input" type="number" placeholder="输入手机号" v-model="account"/>
						<text class="input-suffix-text">同时用作登录账号</text>
					</view>
				</view>
				<view class="form-item">
					<text class="form-label">登录密码:</text>
					<view class="input-wrapper">
						<input class="form-input" type="password" placeholder="输入登录密码" v-model="password"/>
						<text class="input-suffix-text password-hint">6至20位</text>
					</view>
				</view>
				<view class="form-item">
					<text class="form-label">确认密码:</text>
					<view class="input-wrapper">
						<input class="form-input" type="password" placeholder="再次输入登录密码" v-model="password_confirm"/>
						<text class="input-suffix-text password-hint">6至20位</text>
					</view>
				</view>
				<view class="form-item">
					<text class="form-label">昵称或姓名:</text>
					<view class="input-wrapper">
						<input class="form-input" type="text" placeholder="请输入昵称" v-model="nickname"/>
					</view>
				</view>
				<view class="form-item">
					<text class="form-label">真实姓名:</text>
					<view class="input-wrapper">
						<input class="form-input" type="text" placeholder="请输入真实姓名" v-model="real_name"/>
					</view>
				</view>
				<view class="form-item">
					<text class="form-label">性别:</text>
					<view class="input-wrapper" style="border: none;">
						<van-radio-group v-model="sex" direction="horizontal">
						  <van-radio :name="0">保密</van-radio>
						  <van-radio :name="1">男</van-radio>
						  <van-radio :name="2">女</van-radio>
						</van-radio-group>
					</view>
				</view>
				<view class="form-item">
					<text class="form-label">身份证证件号:</text>
					<view class="input-wrapper">
						<input class="form-input" type="text" placeholder="请输入身份证证件号" v-model="id_card"/>
					</view>
				</view>
				<view class="form-item">
					<text class="form-label">邮箱地址:</text>
					<view class="input-wrapper">
						<input class="form-input" type="text" placeholder="请输入邮箱地址" v-model="email"/>
					</view>
				</view>
				<view class="form-item">
					<text class="form-label">联系地址:</text>
					<view class="input-wrapper">
						<input class="form-input" type="text" placeholder="请输入联系地址" v-model="address"/>
					</view>
				</view>
				<view class="form-item">
					<text class="form-label">邮政编码:</text>
					<view class="input-wrapper">
						<input class="form-input" type="text" placeholder="请输入邮政编码" v-model="post_code"/>
					</view>
				</view>
				<button class="submit-btn" @click="refreshPage">提交注册</button>

				<view class="terms-checkbox">
					<van-checkbox v-model="checked"><text class="terms-text">阅读<text class="terms-link">《今生永恒网服务条款》</text>提交注册表示已同意。</text></van-checkbox>
				</view>
			</view>
		</view>
		<Footer></Footer>
	</view>
</template>

<script setup>
	// Vue 3 setup syntax
	// No complex logic needed for just layout还原
	import {
		ref
	} from 'vue';
	import Footer from '@/components/footer/index.vue'
	import Header from '@/components/header/index.vue'
	import {
		register,  login,
	} from '@/api/user';
	import { setToken } from '@/utils/token.js';
	const checked = ref(false)
	const account = ref('')
	const password = ref('')
	const password_confirm = ref('')
	const nickname = ref('')
	const real_name = ref('')
	const sex = ref(0)
	const id_card = ref('')
	const email = ref('')
	const address = ref('')
	const post_code = ref('')
	
	const refreshPage = () => {
		console.log('Refresh page');
		if(!checked.value){
			uni.showToast({
				title: '请阅读并同意服务条款',
				icon: 'none',
				duration: 2000
			})
			return false
		}
		if(account.value == ''){
			uni.showToast({
				title: '请填写手机号',
				icon: 'none',
				duration: 2000
			})
			return false
		}
		if(password.value == ''){
			uni.showToast({
				title: '请填写密码',
				icon: 'none',
				duration: 2000
			})
			return false
		}
		if(password_confirm.value == ''){
			uni.showToast({
				title: '请输入确认密码',
				icon: 'none',
				duration: 2000
			})
			return false
		}
		if(password.value !== password_confirm.value){
			uni.showToast({
				title: '两次密码不一样',
				icon: 'none',
				duration: 2000
			})
			return false
		}
		if(nickname.value == ''){
			uni.showToast({
				title: '请填写昵称',
				icon: 'none',
				duration: 2000
			})
			return false
		}
		if(real_name.value == ''){
			uni.showToast({
				title: '请填写真实姓名',
				icon: 'none',
				duration: 2000
			})
			return false
		}
		if(id_card.value == ''){
			uni.showToast({
				title: '请填写身份证证件号',
				icon: 'none',
				duration: 2000
			})
			return false
		}
		// if(email.value == ''){
		// 	uni.showToast({
		// 		title: '请填写邮箱地址',
		// 		icon: 'none',
		// 		duration: 2000
		// 	})
		// 	return false
		// }
		// if(address.value == ''){
		// 	uni.showToast({
		// 		title: '请填写联系地址',
		// 		icon: 'none',
		// 		duration: 2000
		// 	})
		// 	return false
		// }
		// if(post_code.value == ''){
		// 	uni.showToast({
		// 		title: '请填写邮政编码',
		// 		icon: 'none',
		// 		duration: 2000
		// 	})
		// 	return false
		// }
		let obj = {
			account: account.value,
			password: password.value,
			password_confirm: password_confirm.value,
			nickname: nickname.value,
			real_name: real_name.value,
			sex: sex.value,
			id_card: id_card.value,
			email: email.value,
			address: address.value,
			post_code: post_code.value,
		}
		register(obj).then(async (data) => {
			console.log(data)
			if(data.code == 1){
				uni.showToast({
					title: '注册成功',
					icon: 'none',
					duration: 2000
				})
				
				
				let obj = {
					account: account.value.trim(),
					password: password.value,
					terminal: 3
				}
				login(obj).then(res=>{
					console.log(res)
					if(res.code == 1){
						setToken(res.data.token)
						uni.setStorageSync('userInfo', res.data)
						uni.reLaunch({
							url: '/pages/index/index'
						})
					}else{
						uni.showToast({
							title: res.msg,
							icon: 'none',
							duration: 1000
						})
					}
				}).catch(error=>{
					console.log(error)
				})
			}else{
				uni.showToast({
					title: data.msg,
					icon: 'none',
					duration: 2000
				})
			}
		}).catch((error) => {
			console.log(error)
		})
		
	};
</script>

<style scoped>
	/* Scoped styles for this page */
	.container {
		max-width: 750rpx;
		margin: 0 auto;
		display: flex;
		flex-direction: column;
		min-height: 100vh;
		/* Ensure container is at least viewport height for footer positioning */
		background-color: #f5f5f5;
		/* Match background color */
		padding-bottom: 100rpx;
		/* Add padding at the bottom to prevent content being hidden by floating button */
	}

	/* Custom Header Styles */
	.custom-header {
		display: flex;
		justify-content: space-between;
		align-items: center;
		height: 88rpx;
		/* Standard header height */
		padding: 0 20rpx;
		background-color: #fff;
		border-bottom: 1rpx solid #eee;
	}

	.header-left,
	.header-right {
		width: 80rpx;
		text-align: center;
	}

	.header-icon {
		font-size: 40rpx;
		color: #333;
	}

	.header-center {
		flex-grow: 1;
		text-align: center;
		display: flex;
		flex-direction: column;
		align-items: center;
		overflow: hidden;
		/* Prevent long text from breaking layout */
	}

	.header-title {
		font-size: 30rpx;
		color: #333;
		font-weight: bold;
		white-space: nowrap;
		/* Prevent wrapping */
		overflow: hidden;
		text-overflow: ellipsis;
	}

	.header-subtitle {
		font-size: 20rpx;
		color: #999;
		white-space: nowrap;
		/* Prevent wrapping */
		overflow: hidden;
		text-overflow: ellipsis;
	}

	/* Navigation Bar Styles */
	.nav-bar {
		display: flex;
		justify-content: space-between;
		align-items: center;
		height: 88rpx;
		padding: 0 20rpx;
		background-color: #fff;
		border-bottom: 1rpx solid #eee;
	}

	.nav-left {
		display: flex;
		align-items: center;
	}

	.nav-icon {
		font-size: 40rpx;
		/* Adjust icon size */
		color: #333;
		margin-right: 30rpx;
		/* Spacing between icons */
	}

	.nav-right {
		display: flex;
		align-items: center;
	}

	.nav-link {
		font-size: 28rpx;
		color: #333;
		margin-left: 40rpx;
		/* Spacing between links */
	}

	.nav-link.active {
		color: #ff9800;
		/* Orange color for active/selected link */
		font-weight: bold;
	}

	/* Banner Styles */
	.banner {
		width: 100%;
		height: 300rpx;
		/* Adjust height as needed */
		position: relative;
		overflow: hidden;
	}

	.banner-bg {
		width: 100%;
		height: 100%;
		position: absolute;
		top: 0;
		left: 0;
	}

	.banner-overlay {
		position: absolute;
		top: 0;
		left: 0;
		width: 100%;
		height: 100%;
		display: flex;
		flex-direction: column;
		justify-content: space-between;
		padding: 30rpx 40rpx;
		/* Adjust padding */
		color: #fff;
		/* Text color on banner */
		/* Add a subtle gradient or shadow if needed to make text readable */
		text-shadow: 1rpx 1rpx 2rpx rgba(0, 0, 0, 0.5);
	}

	.banner-title-area {
		display: flex;
		align-items: flex-end;
	}

	.banner-main-title {
		font-size: 50rpx;
		/* Adjust font size */
		font-weight: bold;
		margin-right: 20rpx;
	}

	.banner-sub-area {
		display: flex;
		flex-direction: column;
		font-size: 24rpx;
		/* Adjust font size */
	}

	.banner-url {
		font-size: 20rpx;
		/* Adjust font size */
	}

	.banner-slogan-area {
		display: flex;
		justify-content: flex-end;
		/* Align to the right */
		font-size: 36rpx;
		/* Adjust font size */
	}

	.banner-slogan {
		margin-left: 30rpx;
		/* Spacing between slogans */
		font-weight: bold;
	}

	/* Main Content Styles */
	.main-content {
		padding: 30rpx;
		background-color: #fff;
		margin-top: 30rpx;
		/* Space below banner */
		border-radius: 10rpx;
		margin: 30rpx 20rpx 0 20rpx;
		/* Add side margins and top margin */
		box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
		/* Subtle shadow */
	}

	.page-title {
		font-size: 36rpx;
		font-weight: bold;
		text-align: center;
		margin-bottom: 40rpx;
		color: #333;
	}

	/* Form Styles */
	.form-section {
		margin-bottom: 40rpx;
	}

	.form-item {
		margin-bottom: 30rpx;
	}

	.form-label {
		font-size: 28rpx;
		color: #333;
		margin-bottom: 10rpx;
		display: block;
		/* Make label a block for margin-bottom */
	}

	.input-wrapper {
		position: relative;
		display: flex;
		align-items: center;
		border: 1rpx solid #ddd;
		border-radius: 5rpx;
		padding: 0 20rpx;
		/* Add padding inside the input container */
		height: 80rpx;
		/* Fixed height for inputs */
	}

	.form-input {
		flex: 1;
		/* Allow input to take available space */
		height: 100%;
		/* Fill parent height */
		font-size: 28rpx;
		color: #333;
		/* Remove default input padding/appearance if any */
		-webkit-appearance: none;
		-moz-appearance: none;
		appearance: none;
	}

	/* Placeholder style */
	.form-input::placeholder {
		color: #ccc;
	}

	.input-suffix-text {
		position: absolute;
		right: 20rpx;
		/* Adjust position */
		font-size: 24rpx;
		color: #999;
		pointer-events: none;
		/* Make text non-interactive */
	}

	.input-icon {
		position: absolute;
		right: 50rpx;
		/* Adjust position relative to suffix text */
		font-size: 30rpx;
		/* Adjust icon size */
		color: #ccc;
		/* Icon color */
		pointer-events: none;
	}

	.password-hint {
		right: 50rpx;
		/* Position hint text next to icon */
	}

	.name-hint {
		right: 50rpx;
		/* Position hint text next to icon */
	}


	.verify-code-item {
		display: flex;
		align-items: center;
	}

	.code-input-wrapper {
		flex: 1;
		/* Allow code input to take space */
		border: 1rpx solid #ddd;
		/* Add border back */
		border-radius: 5rpx;
		padding: 0 20rpx;
		height: 80rpx;
		margin-right: 20rpx;
		/* Space between input and button */
	}

	.code-input {
		border: none;
		/* Remove border from input itself if wrapper has border */
		padding: 0;
		/* Remove padding from input itself */
	}

	.get-code-btn {
		/* Uniapp button default styles need override */
		height: 80rpx;
		line-height: 80rpx;
		font-size: 28rpx;
		color: #fff;
		background-color: #ff9800;
		/* Orange color */
		border-radius: 5rpx;
		padding: 0 30rpx;
		border: none;
		/* Remove default button margin/padding */
		margin: 0;
		display: flex;
		/* Use flex to center text */
		align-items: center;
		justify-content: center;
	}

	.submit-btn {
		height: 90rpx;
		line-height: 90rpx;
		font-size: 32rpx;
		color: #fff;
		background-color: #ff9800;
		/* Orange color */
		border-radius: 8rpx;
		/* More rounded corners */
		text-align: center;
		margin-top: 40rpx;
		/* Space above the button */
		border: none;
		margin-bottom: 30rpx;
	}

	.terms-checkbox {
		display: flex;
		align-items: flex-start;
		/* Align text nicely with checkbox */
		font-size: 26rpx;
		color: #666;
	}

	.terms-text {
		flex: 1;
		/* Allow text to wrap */
	}

	.terms-link {
		color: #007aff;
		/* Blue color for link */
		text-decoration: underline;
	}


	/* QR Codes Section */
	.qr-codes-section {
		display: flex;
		justify-content: space-around;
		/* Distribute space evenly */
		padding: 40rpx 0;
		border-top: 1rpx dashed #eee;
		/* Add separator line */
		margin-top: 40rpx;
	}

	.qr-item {
		display: flex;
		flex-direction: column;
		align-items: center;
	}

	.qr-image {
		width: 200rpx;
		/* Fixed width for QR code */
		height: 200rpx;
		/* Fixed height for QR code */
		/* Need actual images */
		background-color: #eee;
		/* Placeholder background */
	}

	.qr-label {
		font-size: 26rpx;
		color: #555;
		margin-top: 10rpx;
	}

	/* Footer Styles */
	.footer {
		margin-top: 40rpx;
		/* Space above footer */
		padding: 40rpx 20rpx 120rpx 20rpx;
		/* Add bottom padding for floating button */
		background-color: #f0f0f0;
		/* Light gray background */
		text-align: center;
	}

	.footer-links {
		font-size: 26rpx;
		color: #555;
		margin-bottom: 20rpx;
	}

	.footer-links text {
		margin: 0 10rpx;
		/* Space between links and separators */
	}

	.footer-copyright {
		font-size: 22rpx;
		color: #888;
		display: flex;
		flex-direction: column;
		align-items: center;
	}

	.footer-copyright text {
		margin-bottom: 5rpx;
		/* Space between copyright lines */
	}

	/* Floating Button */
	.floating-button {
		position: fixed;
		bottom: 50rpx;
		/* Distance from bottom */
		right: 30rpx;
		/* Distance from right */
		background-color: rgba(0, 0, 0, 0.7);
		/* Semi-transparent dark background */
		color: #fff;
		padding: 15rpx 25rpx;
		border-radius: 40rpx;
		/* Pill shape */
		font-size: 26rpx;
		z-index: 100;
		/* Ensure it's on top */
		box-shadow: 0 4rpx 10rpx rgba(0, 0, 0, 0.2);
	}
</style>